<html>                                                                  
 <head> 
 <script src="md5.js" type="text/javascript"></script>                                                                 
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
 <script type="text/javascript">
	function test(){
		testLogin();
		testGetHistory();
		testAddSticky();
		testGetUsers();
		testIsAdmin();
		testGetInfo();
		testIsLoggedIn();
		testPromoteUser();
	}
	function write(s){
		var str = s + "<br />\n";
		$('.test').append(str);
	}
	
	function testIsAdmin(){
		write("Testing isAdmin");
		var info = {};
		var ret = get("userApi.php","isAdmin",info);
		write(ret.admin == 0);
	}
	
	function testGetInfo(){
		write("Testing getInfo");
		var info = {"projectId" : 1};
		var ret = get("userApi.php","getInfo",info);
		write(ret.hasOwnProperty("userIds"));
	}
	
	function testIsLoggedIn(){
		write("Testing isLoggedIn");
		var info = {};
		var ret = get("userApi.php","isLoggedIn",info);
		write(ret.userId >= 0);
	}
	
	function testPromoteUser(){
		write("Testing promoteUser");
		var userID = "4";
		var info = {"userId" : userID};
		var ret = get("userApi.php","promoteUser",info);
		write(ret.admin == 0);
	}
	
	function testGetUsers(){
		write("Testing getUsers");
		var info = {"projectId" : 1};
		var ret = get("projectApi.php","getUsers",info);
		write(ret.hasOwnProperty("userIds"));
		
	}
	function testLogin(){
		write("Testing Login");
		var userName = "smoj33@gmail.com";
		var pw = "aaaa";
		var h_pw = hex_md5(pw);
		var info = {"email" : userName,"password" : h_pw, "remember" : 1}
		var ret = get("userApi.php","login",info);
		write((ret.userId >= 0));
			
		
	}
	function testGetHistory(){
		var send = {"state" : 1};
		var dfs = get("stickyApi.php","getId",send);
	}
	function testAddSticky(){
		write("Testing Add Sticky");
		var info = {
   			"state_id" : 1,
   			"priority_id": 1,
			"release_id": 1,
   			"note": "This is a test",
   			"title": "Title here",
   			"color": "#FFFFFF",
   			"severity_id" : 1,
   			"defect_state_id" : 0
		}
		var ret = get("stickyApi.php","addSticky",info);
		if(ret.hasOwnProperty("errors")){
			write(ret.errors.hasOwnProperty("category"));
		}
		info["category"] = "story2";
		var ret = get("stickyApi.php","addSticky",info);
		if(ret.hasOwnProperty("errors")){
			write(ret.errors.hasOwnProperty("category"));
		}
		info["category"] = "story";
		info["state_id"] = 1000;
		var ret = get("stickyApi.php","addSticky",info);
		if(ret.hasOwnProperty("errors")){
			write(ret.errors.hasOwnProperty("state_id"));
		}
		info["state_id"] = 1;
		info["release_id"] = 1000;
		var ret = get("stickyApi.php","addSticky",info);
		if(ret.hasOwnProperty("errors")){
			write(ret.errors.hasOwnProperty("release_id"));
		}
			
	}
	function get(api,method,data){
		var json_data = JSON.stringify(data);
		var params = {"__m" : method, "__d" : json_data};
		var my_url = "/lib/" + api;
		var retVal = null;
		$.ajax({url: my_url,async: false,data: params,success: ret});
		function ret(data){
			console.log(data);
			retVal = eval("(" + data +  ")");
		}
		return retVal;
	}   
 </script>                                                               
 </head>                                                                 
 <body onLoad="test()">                                                                  
   <!-- we will add our HTML content here -->
	<div class="test"></div>                                     
 </body>                                                                 
 </html>

